import { useAddKey } from '@/hooks/useAddKey';
import { getFanHouse, fang_rental } from '@/api/index';
const kefu = {
  namespace: 'kefu',
  state: {
    name: '张三',
    listData: [],
    rentalList: [],
  },
  subscriptions: {
    INITHOUSE({ dispatch }) {
      dispatch({
        type: 'INITDATA',
      });
    },
    INITRENTAL({ dispatch }) {
      dispatch({
        type: 'INITRENTAL1',
      });
    },
  },
  effects: {
    *INITDATA(action, { call, put }) {
      const data = yield call(getFanHouse);
      yield put({ type: 'INIT', payload: data.data.data });
    },
    *INITRENTAL1(action, { call, put }) {
      const data = yield call(fang_rental);
      console.log(data);
      yield put({ type: 'INITRENTAL2', payload: data.data.RECORDS });
    },
  },
  reducers: {
    EDITDETAIL(state, { data }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.listData.forEach((item) => {
        if (item.id == data.id) {
          item.title = data.title;
          return item;
        }
      });
      console.log(newState.listData);
      return { ...newState };
    },
    EDITRENTALDETAIL(state, { data }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.rentalList.forEach((item) => {
        if (item.id == data.id) {
          item.title = data.title;
          return item;
        }
      });
      console.log(newState.listData);
      return { ...newState };
    },
    ADDHOUSES(state, { payload }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.listData.push({
        address: payload.address,
        agent_id: 0,
        broker_id: 0,
        city: 4,
        complate_time: 0,
        complate_time_memo: null,
        create_time: 1522579131,
        developer_id: 222,
        developer_name: null,
        discount: null,
        hits: 607,
        id: newState.listData[newState.listData.length - 1].id + 1,
        img: '/uploads/house/20181102/6f367596a4cffa4ff1e94a8bf6172a98.png',
        is_discount: 0,
        key: newState.listData[newState.listData.length - 1].id + 1,
        lat: '20.021263625269',
        license_key: null,
        lng: '110.30991946701',
        opening_time: 0,
        opening_time_memo: null,
        ordid: 1000,
        pano_url: 'http://www.njtxfc.com/njpano/bgyfhc/tour.html',
        price: 22000,
        ratio: 83.3,
        rec_position: 0,
        red_packet: 0,
        renovation: 0,
        sale_address: null,
        sale_phone: '{"phone":"","extension":""}',
        sale_status: 1,
        status: 0,
        tags_id: '0',
        title: payload.title,
        type_id: '0',
        unit: 1,
        update_time: new Date().getMinutes(),
        video: null,
        voice: 1,
        voiceurl: null,
      });
      return { ...newState };
    },
    DEL(state, { payload }) {
      console.log(payload);
      let newState = JSON.parse(JSON.stringify(state));
      newState.listData.forEach((item, index) => {
        if (item.id == payload.id) {
          newState.listData.splice(index, 1);
        }
      });
      return { ...newState };
    },
    INITRENTAL2(state, { payload }) {
      console.log(payload);
      let newState = JSON.parse(JSON.stringify(state));
      newState.rentalList = useAddKey(payload);
      return {
        ...newState,
      };
    },
    INIT(state, { payload }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.listData = useAddKey(payload);
      return {
        ...newState,
      };
    },
    SEARCH(state, { payload }) {
      console.log(payload);
      let newState = JSON.parse(JSON.stringify(state));
      newState.listData = newState.listData.filter((item) => {
        if (
          item.status === (payload.status === '启用' ? 1 : 0) &&
          item.title.includes(payload.title)
        ) {
          return item;
        }
      });
      return {
        ...newState,
      };
    },
    SEARCH1(state, { payload }) {
      console.log(payload);
      let newState = JSON.parse(JSON.stringify(state));
      newState.rentalList = newState.rentalList.filter((item) => {
        if (
          item.status === (payload.status === '正常' ? 1 : 0) &&
          item.title.includes(payload.title)
        ) {
          return item;
        }
      });
      console.log(newState.rentalList);
      return {
        ...newState,
      };
    },
    RENTALADD(state, { payload }) {
      console.log(payload);
      let newState = JSON.parse(JSON.stringify(state));
      newState.rentalList.push({
        address: payload.address,
        agent_id: 0,
        broker_id: 0,
        city: 4,
        complate_time: 0,
        complate_time_memo: null,
        create_time: 1522579131,
        developer_id: 222,
        developer_name: null,
        discount: null,
        hits: 607,
        id: newState.rentalList[newState.rentalList.length - 1].id + 1,
        img: '/uploads/house/20181102/6f367596a4cffa4ff1e94a8bf6172a98.png',
        is_discount: 0,
        key: newState.rentalList[newState.rentalList.length - 1].id + 1,
        lat: '20.021263625269',
        license_key: null,
        lng: '110.30991946701',
        opening_time: 0,
        opening_time_memo: null,
        ordid: 1000,
        pano_url: 'http://www.njtxfc.com/njpano/bgyfhc/tour.html',
        price: 22000,
        ratio: 83.3,
        rec_position: 0,
        red_packet: 0,
        renovation: 0,
        sale_address: null,
        sale_phone: '{"phone":"","extension":""}',
        sale_status: 1,
        status: 0,
        tags_id: '0',
        title: payload.title,
        type_id: '0',
        unit: 1,
        update_time: new Date().getMinutes(),
        video: null,
        voice: 1,
        voiceurl: null,
      });
      return { ...newState };
    },
  },
};

export default kefu;
